package org.aurora.common.serialization;

import com.alibaba.fastjson.JSON;

import java.nio.charset.StandardCharsets;

public class FastjsonUtf8Byte2ObjectSerialization implements Byte2ObjectSerialization {
    @Override
    public byte[] serialization(Object o) {
        if (o == null) {
            return null;
        }
        String dataStr = JSON.toJSONString(o);
        return dataStr.getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public <T> T deserialization(byte[] bytes, Class<T> clazz) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }

        String dataStr = new String(bytes, StandardCharsets.UTF_8);

        return JSON.parseObject(dataStr, clazz);
    }
}
